<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
//use Jialeo\LaravelSchemaExtend\Schema;
class CreateCmsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(\App\Utils\Enums\TableEnums::CMS_CATEGORIES, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->default('');
            $table->unsignedInteger('pid')->default(0);
            $table->string('model_id', 255)->nullable()->comment('模型编号，多个用逗号隔开');
            $table->unsignedInteger('order')->default(0);
            $table->string('title')->default('');
            $table->string('keywords')->default('');
            $table->string('description')->default('');
            $table->timestamps();
            $table->softDeletes();
            $table->unique(['pid', 'name', 'model_id']);
            $table->comment = '内容CMS：栏目表';
        });

        Schema::create(\App\Utils\Enums\TableEnums::CMS_DOCUMENTS, function (Blueprint $table) {
            $table->increments('id');
            $table->char('name',32)->unique()->comment('文档唯一标识 根据规则自动生成');
            $table->string('lang',20)->default('cn')->comment('语言类型');
            $table->integer('pid')->default(0)->comment('上级文档编号');
            $table->string('title')->default('')->comment('文档标题');
            $table->string('keyword')->default('')->comment('关键字');
            $table->string('description')->default('')->comment('简介');
            $table->smallInteger('category_id')->default(0)->comment('栏目编号');
            $table->string('cover_image', 255)->nullable()->comment('封面图片');
            $table->integer('entity_id')->default(0)->comment('文档模型编号');
            $table->char('entity_name', 20)->default('')->comment('文档类型 多个值用逗号隔开! 默认值： news:资讯; photo:图集; page:单网页模型');
            $table->smallInteger('click', false, true)->default(0)->comment('点击量');
            $table->smallInteger('support', false, true)->default(0)->comment('支持数');
            $table->smallInteger('comment', false, true)->default(0)->comment('评论数');
            $table->smallInteger('collect', false, true)->default(0)->comment('搜藏数');
            $table->smallInteger('sort', false, true)->default(0)->comment('排序 由大到小');
            $table->tinyInteger('status')->default(0)->comment('文档状态 默认 0 未发布; -1:下架; 99:发布; 1:1级审核通过; 2:2级审核通过;');
            $table->tinyInteger('audit', false, false)->default(0)->comment('是否需要审核 0:不需要审核; 1:需要1级审核; 2:需要2级审核;');
            $table->dateTime('fixed_release_time')->nullable()->comment('定时发布时间');
            $table->json('extend')->nullable()->comment('扩展字段');
            $table->integer('admin_user_id', false, true)->default(0)->comment('创建人编号');
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '内容CMS：基础文档表';
        });

        Schema::create(\App\Utils\Enums\TableEnums::CMS_COMMENTS, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id')->default(0);
            $table->unsignedTinyInteger('is_admin')->default(0);
            $table->unsignedInteger('entity_id')->default(0);
            $table->unsignedInteger('content_id')->default(0);
            $table->string('content', 1024)->default('');
            $table->unsignedInteger('like')->default(0);
            $table->unsignedInteger('dislike')->default(0);
            $table->unsignedInteger('grade')->default(0);
            $table->unsignedInteger('pid')->default(0)->comment('父ID。方便获取评论的层级关系');
            $table->unsignedInteger('rid')->default(0)->comment('根ID。方便获取某条评论下的所有评论');
            $table->unsignedTinyInteger('status')->default(0);
            $table->unsignedInteger('reply_count')->default(0);
            $table->unsignedInteger('reply_user_id')->default(0);
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '内容CMS：评论表';
        });

        Schema::create(\App\Utils\Enums\TableEnums::CMS_COMMENT_OPERATE_LOGS, function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->default(0);
            $table->unsignedInteger('comment_id')->default(0);
            $table->string('operate', 20)->default('');
            $table->timestamps();

            $table->index('user_id');
            $table->softDeletes();
            $table->comment = '内容CMS：';
        });

        Schema::create(\App\Utils\Enums\TableEnums::CMS_TAGS, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 20)->unique();
            $table->timestamps();
            $table->softDeletes();
            $table->comment = '内容CMS：标签表';
        });

        Schema::create(\App\Utils\Enums\TableEnums::CMS_CONTENT_TAGS, function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('entity_id')->unsigned();
            $table->integer('content_id')->unsigned();
            $table->bigInteger('tag_id')->unsigned();
            $table->timestamps();

            $table->index('entity_id', 'content_id');
            $table->index('entity_id', 'tag_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::CMS_CATEGORIES);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::CMS_DOCUMENTS);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::CMS_COMMENTS);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::CMS_COMMENT_OPERATE_LOGS);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::CMS_TAGS);
        Schema::dropIfExists(\App\Utils\Enums\TableEnums::CMS_CONTENT_TAGS);
    }
}
